<template>
  <div>
    <ContentWrap>
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="排序方式" prop="sortType">
          <el-select clearable v-model="queryParams.sortType" placeholder="请选择" class="!w-200px">
            <el-option
              v-for="dict in getIntDictOptions(DICT_TYPE.BID_SORT_TYPE)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input
            clearable
            :maxlength="50"
            v-model="queryParams.customerName"
            placeholder="请输入客户名称"
            @keyup.enter="handleQuery"
            class="!w-200px"
          />
        </el-form-item>
        <el-form-item label="产品名称" prop="coalType">
          <el-select
            multiple
            clearable
            v-model="queryParams.coalType"
            placeholder="请选择"
            class="!w-300px"
          >
            <el-option
              v-for="dict in coalTypeList"
              :key="dict.id"
              :label="dict.coalName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="选择时间" prop="localDates">
          <el-date-picker
            v-model="queryParams.localDates"
            value-format="YYYY-MM-DD"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            :disabled-date="getDisabledStatus"
            type="daterange"
            class="!w-250px"
          />
        </el-form-item>

        <el-form-item>
          <el-button @click="handleQuery">
            <Icon icon="ep:search" class="mr-5px" />
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" class="mr-5px" />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <ContentWrap>
      <el-container class="flex space-x-12">
        <el-card>
          <div class="font-bold text-center">合计采购量</div>
          <p class="mt-2">{{ totalMap.purchaseTotal }}万吨</p>
        </el-card>
        <el-card>
          <div class="font-bold text-center">合计金额</div>
          <p class="mt-2">{{ totalMap.purchaseAmountTotal }}万元</p>
        </el-card>
      </el-container>
    </ContentWrap>
    <ContentWrap class="!mb-0">
      <!-- 列表 -->
      <el-table
        v-loading="loading"
        :data="list"
        border
        stripe
        :height="445"
        :header-cell-style="{ background: '#F3F4F7', color: '#555' }"
      >
        <el-table-column label="排名" align="center" width="60" fixed="left">
          <template #default="{ $index }">
            {{ $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column
          label="客户名称"
          align="center"
          prop="customerName"
          min-width="200"
          fixed="left"
          :show-overflow-tooltip="true"
        />
        <el-table-column label="采购量(万吨)" align="center" prop="purchaseTotal" width="150" />
        <el-table-column
          label="总金额(万元)"
          align="center"
          prop="purchaseAmountTotal"
          width="150"
        />
        <el-table-column label="平均价格(元/吨)" align="center" prop="averagePrice" width="150" />
        <el-table-column
          label="平均起拍价(元/吨)"
          align="center"
          prop="averageStartPrice"
          min-width="150"
        />
        <el-table-column
          label="保证金合计(万元)"
          align="center"
          prop="marginTotal"
          min-width="150"
        />
        <el-table-column
          label="保证金扣款合计(万元)"
          align="center"
          prop="marginDeductionTotal"
          min-width="150"
        />
        <el-table-column
          label="充值合计(万元)"
          align="center"
          prop="rechargeAmount"
          min-width="150"
        />
        <el-table-column
          label="扣款合计(万元)"
          align="center"
          prop="deductionAmount"
          min-width="150"
        />
      </el-table>
    </ContentWrap>
  </div>
</template>
<script setup lang="ts" name="customerRank">
import * as Utils from '@/utils/dict'
import * as dataCenter from '@/api/dataCenter'
import { cloneDeep } from 'lodash-es'
import { getDisabledStatus } from '@/utils/common'

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const queryFormRef = ref()
const queryParams = reactive({
  sortType: undefined,
  customerName: undefined,
  coalType: [],
  localDates: []
})
const totalMap = ref({
  purchaseAmountTotal: 0,
  purchaseTotal: 0
})
const coalTypeList: Record<string, any> = ref([])
const { DICT_TYPE, getIntDictOptions } = reactive(Utils)

/** 查询列表 */

const getList = async () => {
  loading.value = true
  try {
    const params: Record<string, any> = cloneDeep(queryParams)
    if (params.coalType?.length) {
      params.coalId = params.coalType.join(',')
    }
    if (!params?.sortType) {
      delete params?.sortType
    }
    delete params.coalType
    const data = await dataCenter.getCustomerPageByRank(params)
    totalMap.value.purchaseAmountTotal = data?.purchaseAmountTotal || 0
    totalMap.value.purchaseTotal = data?.purchaseTotal || 0
    list.value = data?.customerStatisticsRankRespVOS || []
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const bidCoalManageList = async () => {
  const res = await dataCenter.bidCoalManageList()
  coalTypeList.value = res || []
}

/** 初始化 **/
onMounted(() => {
  bidCoalManageList()
  getList()
})
</script>
